<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\controller\Base;
use think\Request;
use think\Session;
use think\Db;
use think\View;
class Order extends Base
{
    public function order()
    {

        header("content-type:text/html;charset=UTF-8");

        $where = array();

        $order_sn = input("get.order_sn");
        $consignee = input("get.consignee");
        if(!empty($order_sn)){

            $where['order_sn'] = $order_sn;
        }
  

            $data = Db("order")->where($where)->select();
            $page = input("get.page")?input("get.page"):1;
            $pageSize = 5;
            $offset = ($page-1)*$pageSize;
            $totalRows = count($data);
            $totalPage = ceil($totalRows/$pageSize);




        

        $data['data'] = Db("order")->where($where)->limit($offset,$pageSize)->select();

        foreach ($data['data'] as $key => $value) {
            
            $data['data'][$key]['addtime'] = date("Y-m-d H:i:s",$value['addtime']);
        }
       $data['pagess'] = $this->showPage($page,$totalPage,$sep="");
        // echo "<pre>";
        // print_r($data);die;
                    if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){

                        echo json_encode($data);
                    }else{

                        $this->assign("data",$data);
                        return $this->fetch();
                    }    
        // $this->assign("data",$data);
        // return $this->fetch();
    }

    public function ordershow()
    {   

        $order_id = input("get.order_id");
        $res['data'] = Db("order")->where("order_id", $order_id)->select();
        $res['goods']=Db("goods_order")->where("order_id", $order_id)->select();
       // var_dump($res['data']);die;
        return $this->fetch('ordershow',['data'=>$res]);                   
    }

    public function orderInquiry()
    {
        return view('Order/orderInquiry');
    }

     public function showPage($page,$totalPage,$sep=" "){
        $url = $_SERVER ['PHP_SELF'];           //获取当前路径

        $index = ($page == 1) ? "首页" : "<a href='{$url}?page=1' class='btn'>首页</a>";
        $last = ($page == $totalPage) ? "尾页" : "<a href='{$url}?page={$totalPage}' class='btn'>尾页</a>";

        $prevPage=($page>=1)?$page-1:1;
        $nextPage=($page>=$totalPage)?$totalPage:$page+1;
        $prev = ($page == 1) ? "上一页" : "<a href='{$url}?page={$prevPage}' class='btn' style='width:50px;'>上一页</a>";
        $next = ($page == $totalPage) ? "下一页" : "<a href='{$url}?page={$nextPage}' class='btn' style='width:50px'>下一页</a>";

        $str = "总共{$totalPage}页/当前是第{$page}页";

        // $old_page = $page;
        // if($old_page<=6){

        //     $old_page = 6;
        // }else{

        //     $old_page = $page;
        // }

        $p="";
        for($i = $page; $i <= $totalPage; $i ++) {
            //当前页无连接
            if ($page == $i) {
                $p .= "[{$i}]";
            } else {
                $p .= "<a href='{$url}?page={$i}'>[{$i}]</a>";
            }
        }
        
        $pageStr=$str.$sep . $index .$sep. $prev.$sep. $p.$sep .$next.$sep . $last;
        return $pageStr;    //返回分页字符串
    }

}